package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.mapper.SightsReviewMapper;
import com.chixing.pojo.*;
import com.chixing.service.SightsReviewService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SightsReviewServiceImpl implements SightsReviewService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private SightsReviewMapper sightsReviewMapper;
    @Override
    public ServerResponse getSightsReview(Integer sightsId,Integer pageNum) {
        SightsReviewExample sightsReviewExample = new SightsReviewExample();
        sightsReviewExample.createCriteria().andTravelSightsIdEqualTo(sightsId);


        ArrayList<SightsCustAndReviewVO> listVo = new ArrayList<>();
        Page page = PageHelper.startPage(pageNum,3);
        List<SightsReview> sightsReviews = sightsReviewMapper.selectByExample(sightsReviewExample);
        for (SightsReview sightsReview:sightsReviews
             ) {
            SightsCustAndReviewVO sightsCustAndReviewVO = new SightsCustAndReviewVO();
            sightsCustAndReviewVO.setSightsReview(sightsReview);
            Customer customer = customerMapper.selectByPrimaryKey(sightsReview.getCustId());
            sightsCustAndReviewVO.setCustomer(customer);
            listVo.add(sightsCustAndReviewVO);
            System.out.println(sightsReview);
        }
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(listVo);
        if (sightsReviews.size()>0){
            return ServerResponse.getSuccess(pageInfo);
        }else {
            return ServerResponse.getFail(null);
        }
    }
}
